package com.mca.framework.completableFuture;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.*;

/**
 * @Author: zhoupeng
 * @createTime: 2025年07月11日 10:31:53
 * @version: 1.0
 * @Description:
 * @copyright:
 */
public class ThenCombineDemo {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ExecutorService executorService = Executors.newFixedThreadPool(100);
        //异步任务1
        CompletableFuture<List<String>> future1 = CompletableFuture.supplyAsync(() -> {
            //子任务
            try {
                TimeUnit.SECONDS.sleep(3);
                System.out.println(Thread.currentThread().getName());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("future1:" + Thread.currentThread().getName());
            return Arrays.asList("hello","6666");
        }, executorService);
        //异步任务2
        CompletableFuture<List<String>> future2 = CompletableFuture.supplyAsync(() -> {
            //子任务
            try {
                TimeUnit.SECONDS.sleep(1);
                System.out.println(Thread.currentThread().getName());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("future2:" + Thread.currentThread().getName());
            return Arrays.asList("itheima","8888");
        }, executorService);

        //异步任务3
        CompletableFuture<List<String>> future3 = CompletableFuture.supplyAsync(() -> {
            //子任务
            try {
                TimeUnit.SECONDS.sleep(1);
                System.out.println(Thread.currentThread().getName());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("future3:" + Thread.currentThread().getName());
            return Arrays.asList("world","9999");
        }, executorService);
        //当多个异步任务【全部】 执行完毕后，触发后续的任务处理
        CompletableFuture<Object> future = future1.thenCombineAsync(future2, (f1, f2) -> {
             System.out.println(Thread.currentThread().getName());
             return f1 + "  666 " + f2;
        }, executorService).thenCombineAsync(future3,(intermediate,f3) ->{
            return intermediate + f3;
        },executorService);
        System.out.println(future.get());
        executorService.shutdown();

    }
    /***
     *  // 创建线程池对象
     *         Executor executor = executorConfig.asyncServiceExecutor();
     *         // TODO 查询门店时间节点信息
     *         CompletableFuture<Map<String, List<StoreTimeNodeDTO>>> r1 = CompletableFuture.supplyAsync(() -> {
     *             List<StoreTimeNodeDTO> timeNodeList = storeCoreServerRemoteFacade.batchQueryStoreTimeNodes(storeCodes);
     *             return timeNodeList.stream().collect(Collectors.groupingBy(StoreTimeNodeDTO::getStoreCode));
     *         }, executor).exceptionally(e -> {
     *             log.error("查询门店时间节点信息异常，e={}", e);
     *             throw new RomeException(ExceptionType.BATCH_QUERY_STORE_TIME_NODE_EXCEPTION.getCode()
     *                     ,ExceptionType.BATCH_QUERY_STORE_TIME_NODE_EXCEPTION.getName());
     *         });
     *
     *         // TODO 查询门店负责人信息
     *         CompletableFuture<Map<String, List<StoreCodeAndNameDTO>>> r2 = CompletableFuture.supplyAsync(() -> {
     *             List<StoreCodeAndNameDTO> managerList = storeCoreServerRemoteFacade.batchQueryStoreManagers(storeCodes);
     *             return managerList.stream().collect(Collectors.groupingBy(StoreCodeAndNameDTO::getStoreCode));
     *         }, executor).exceptionally(e -> {
     *             log.error("查询门店负责人信息异常，e={}", e);
     *             throw new RomeException(ExceptionType.BATCH_QUERY_STORE_MANAGER_EXCEPTION.getCode(),
     *                     ExceptionType.BATCH_QUERY_STORE_MANAGER_EXCEPTION.getName());
     *         });
     *
     *         // TODO 查询门店所属区域信息
     *         CompletableFuture<Map<String, List<StoreCodeAndNameDTO>>> r3 = CompletableFuture.supplyAsync(() -> {
     *             List<StoreCodeAndNameDTO> areaList = storeCoreServerRemoteFacade.batchQueryStoreAreas(storeCodes);
     *             return areaList.stream().collect(Collectors.groupingBy(StoreCodeAndNameDTO::getStoreCode));
     *         }, executor).exceptionally(e -> {
     *             log.error("查询门店所属区域信息异常，e={}", e);
     *             throw new RomeException(ExceptionType.BATCH_QUERY_STORE_AREA_EXCEPTION.getCode(),
     *                     ExceptionType.BATCH_QUERY_STORE_MANAGER_EXCEPTION.getName());
     *         });
     *
     *         // TODO 查询门店所属组织机构信息
     *         CompletableFuture<Map<String, List<StoreCodeAndNameDTO>>> r4 = CompletableFuture.supplyAsync(() -> {
     *             List<StoreCodeAndNameDTO> orgList = storeCoreServerRemoteFacade.batchQueryStoreOrgs(storeCodes);
     *             return orgList.stream().collect(Collectors.groupingBy(StoreCodeAndNameDTO::getStoreCode));
     *         }, executor).exceptionally(e -> {
     *             log.error("查询门店所属组织机构信息异常，e={}", e);
     *             throw new RomeException(ExceptionType.BATCH_QUERY_STORE_ORG_EXCEPTION.getCode(),
     *                     ExceptionType.BATCH_QUERY_STORE_MANAGER_EXCEPTION.getName());
     *         });
     *
     *         // TODO 查询门店供应商信息
     *         CompletableFuture<Map<String, List<SuppliersResDTO>>> r5 = CompletableFuture.supplyAsync(() -> {
     *             List<SuppliersResDTO> supplierList = storeCoreServerRemoteFacade.batchQueryStoreSuppliers(storeCodes);
     *             return supplierList.stream().collect(Collectors.groupingBy(SuppliersResDTO::getStoreCode));
     *         }, executor).exceptionally(e -> {
     *             log.error("查询门店供应商信息异常，e={}", e);
     *             throw new RomeException(ExceptionType.BATCH_QUERY_STORE_SUPPLIER_EXCEPTION.getCode(),
     *                     ExceptionType.BATCH_QUERY_STORE_SUPPLIER_EXCEPTION.getName());
     *         });
     *
     *         // 获取门店时间节点信息
     *         Map<String, List<StoreTimeNodeDTO>> timeNodeMap = r1.join();
     *         // 获取门店负责人信息
     *         Map<String, List<StoreCodeAndNameDTO>> managerMap = r2.join();
     *         // 获取门店所属区域信息
     *         Map<String, List<StoreCodeAndNameDTO>> areaMap = r3.join();
     *         // 获取门店所属组织机构信息
     *         Map<String, List<StoreCodeAndNameDTO>> orgMap = r4.join();
     *         // 获取门店供应商信息
     *         Map<String, List<SuppliersResDTO>> supplierMap = r5.join();
     *
     *         storeResDTOList.forEach(s -> {
     *             // 设置指定门店下的时间节点信息
     *             s.setTimeNodes(timeNodeMap.get(s.getCode()));
     *             // 设置指定门店下的负责人信息
     *             s.setManagers(managerMap.get(s.getCode()));
     *             // 设置指定门店下的所属区域信息
     *             s.setAreas(areaMap.get(s.getCode()));
     *             // 设置指定门店下的所属组织机构信息
     *             s.setOrganizations(orgMap.get(s.getCode()));
     *             // 设置指定门店下的供应商信息
     *             s.setSuppliers(supplierMap.get(s.getCode()));
     *         });
     */
}
